<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Model\User;
use Validator;
class IndexController extends Controller
{
	//后台首页展示
    public function index()
    {
    	return view('admin.index');
    }

    //修改密码页面
   	public function editpass(Request $request)
   	{
   		if($input=$request->input()){
   			// dd($input);
   			$rules=[
   				'password'=>'required|between:6,20|confirmed',
   			];
   			$message=[
   				'password.required'=>'新密码不能为空!',
   				'password.between'=>'新密码必须是6到20位!',
   				'password.confirmed'=>'新密码和确认密码不一致!',
   			];
   			$vali=Validator::make($input,$rules,$message);
   			if($vali->passes()){
   				$user=User::first();
   				if(md5($input['oldpassword'])==$user->user_pass){
   					$user->user_pass=md5($input['password']);
   					$user->save();
   					// return redirect('admin/index');
   					return back()->with('errors','密码更改成功!');
   				}else{
   					return back()->with('errors','原密码错误');
   				}
   			}else{
   				//返回并且传递出错误的信息
   				return back()->withErrors($vali);
   			}
   		}
   		return view('admin.login.pass');
   	}
}
